<template>
    <div class="order-tab">
        <nut-tabs v-model="tab" size="normal" title-scroll title-gutter="10" name="tab" @click="changeTab">
            <nut-tab-pane v-for="item in tabList" :key="item.key" :pane-key="item.key" :title="item.title">
                <div class="order-list" v-if="orderList.length > 0">
                    <div class="order-item" v-for="(itemVal, index) in orderList"
                        @click="jumpTo('/packageA/pages/serOrderList/details?id=' + itemVal.ID)">
                        <div class="order-no flex-a flex-b">
                            <div>订单编号：{{ itemVal.order_No }}</div>
                            <div>{{ itemVal.status == '0' ? '待付款' : itemVal.status == '1' ? '已付款' : itemVal.status == '3' ? '待服务'
                : itemVal.status == '4' ? '进行中' : itemVal.status == '5' ? '已完成' :
                    itemVal.status == '6' ? '待评价' : itemVal.status == '-1' ? '待退款'
                        : itemVal.status == '-2' ? '已退款' : itemVal.status == '-3' ? '已取消' : '' }}
                            </div>
                        </div>
                        <div class="order-head flex-a flex-b">
                            <div class="left">
                                <img :src="itemVal.pageSetting.remark" alt="">
                            </div>
                            <div class="right">
                                <div class="title">
                                    <div>{{ itemVal.pageSetting.project_Name }}</div>
                                </div>
                                <div class="time">预约时间：{{ itemVal.service_Time }}</div>
                                <div class="price flex-a">
                                    <!-- <div>优惠￥{{ itemVal.less }}</div> -->
                                    <div class="flex">实付<span>￥{{ itemVal.service_Amount }}</span></div>
                                </div>
                            </div>
                        </div>
                        <div class="order-foot flex-a">
                            <nut-button plain color="#DEDEDE" size="small" v-if="itemVal.status == '0'"
                                @click.stop="resetOrder(itemVal)">取消订单</nut-button>
                            <nut-button color="#21C38F" size="small" v-if="itemVal.status == '0'" @click.stop="toPay(itemVal)">立即支付</nut-button>
                        </div>
                    </div>
                </div>
                <nut-empty description="暂无数据" v-else></nut-empty>
            </nut-tab-pane>
        </nut-tabs>
    </div>
</template>

<script lang="ts">
import Taro, { useRouter } from '@tarojs/taro';
import { useReachBottom } from '@tarojs/taro';
import { Left, MoreX } from '@nutui/icons-vue-taro';
import { reactive, toRefs, onMounted } from 'vue';
import { getOrderList, pay, changeOrder } from '../../../../api/index'
export default {
    components: {
        Left,
        MoreX
    },
    setup() {
        const data = reactive({
            imgUrl:
                'https://cxj.szzkba.cn/upload/i/2024/06/04/182133.jpeg',
            waybillToken: '',
            tab: 'null' as any,
            orderList: [
                // {
                //     orderNo: '354612348945612',
                //     status: 1,
                //     url: 'https://cxj.szzkba.cn/upload/i/2024/07/23/141815.png',
                //     title: '上门维修水龙头',
                //     time: '2021-09-19 10:00',
                //     less: '104',
                //     price: '99.9',
                // }
            ] as any[],
            tabList: [
                {
                    title: '全部',
                    key: 'null',
                },
                {
                    title: '待付款',
                    key: 0,
                },
                {
                    title: '待接单',
                    key: 1,
                },
                {
                    title: '待服务',
                    key: 3,
                },
                {
                    title: '进行中',
                    key: 4,
                },
                {
                    title: '已完成',
                    key: 5
                },
                {
                    title: '已取消',
                    key: -3
                },
            ],
            page: 1,
            text: 'DPK364116621912',
            showBottom: false,
            refOrderNo: '',
            tkNum: '',
            token: '',
            user: {}
        });

        onMounted(() => {
            const router = useRouter();
            Taro.getStorage({
                key: 'user',
                success: function (res) {
                    data.user = res.data ? res.data : {}
                    getOrder()
                }
            })
        })
        const changeTab = (title) => {
            data.tab = title.paneKey
            data.page = 1
            data.orderList = []
            getOrder()
        }
        const getOrder = () => {
            getOrderList({ user_ID: data.user.uuid, page: data.page, pageSize: 10, status: data.tab }).then(res => {
                if (res.code == 0) {
                    let list = res.data.list
                    list.forEach(item => {
                        item.CreatedAt = reTime(item.CreatedAt)
                    });
                    data.orderList = [...data.orderList, ...list]
                    console.log(data.orderList)
                }
            })
        }
        const resetOrder = (item) => {
            changeOrder({ order_No: item.order_No, status: -3 }).then(res => {
                if (res.code == 0) {
                    getOrder()
                }
            })
        }
        const addZero = (n) => {
            return n < 10 ? '0' + n : n
        }
        const reTime = (t) => {
            let time = new Date(t)
            let y = time.getFullYear()
            let m = addZero(time.getMonth() + 1)
            let d = addZero(time.getDate())
            let h = addZero(time.getHours())
            let f = addZero(time.getMinutes())
            let s = addZero(time.getSeconds())
            return y + '-' + m + '-' + d + ' ' + h + ':' + f + ':' + s
        }
        const toPay = (item) => {
            pay({
                order_No: item.order_No,
                openId: data.user.openId,
                title: '支付调用',
                remark: '',
            }).then(res => {
                if (res.code == 0) {
                    Taro.requestPayment({
                        timeStamp: res.data.timeStamp,
                        nonceStr: res.data.nonceStr,
                        package: res.data.package,
                        signType: res.data.signType,
                        paySign: res.data.paySign,
                        success: function (res) {
                            getOrder(data.tab)
                        },
                    })
                }
            })
        }
        const jumpTo = (path) => {
            Taro.navigateTo({ url: path });
        }
        useReachBottom(() => {
            //console.log('onReachBottom')
            data.page++
            getOrder()
        })
        return {
            ...toRefs(data), changeTab, jumpTo, getOrder, toPay, reTime, addZero, resetOrder
        }
    }
};
</script>
<style lang="scss">
.order-tab {

    .nut-tab-pane {
        padding: 0;
    }

    .order-list {
        padding: 20rpx 15rpx 20rpx 15rpx;
        background: #E9E9E9;

        .order-item {
            background: #fff;
            border-radius: 10rpx;
            box-sizing: border-box;
            padding: 20rpx;
            margin-bottom: 20rpx;

            .order-no {
                margin-bottom: 20rpx;

                div:first-child {
                    font-size: 24rpx;
                }

                div:last-child {
                    color: #FE6F49;
                }
            }

            .order-head {
                padding-bottom: 20rpx;
            }

            .left {
                width: 23%;

                img {
                    width: 150rpx;
                    height: 150rpx;
                    border-radius: 10rpx;
                    float: left;
                }
            }

            .right {
                width: 74%;

                .title {
                    margin-bottom: 20rpx;

                    div:first-child {
                        font-size: 28rpx;
                        font-weight: bold;
                    }

                }

                .time {
                    color: #666;
                    font-size: 26rpx;
                    margin-bottom: 20rpx;
                }

                .price {
                    div:first-child {
                        font-size: 26rpx;
                        margin-right: 40rpx;
                    }

                    div:last-child {
                        font-size: 26rpx;

                        span {
                            color: #F22F5C;
                            font-size: 32rpx;
                            font-weight: bold;
                        }
                    }
                }
            }

            .order-foot {
                border-top: 1rpx solid #F2F2F2;
                padding-top: 20rpx;
                justify-content: flex-end;

                .nut-button--plain {
                    margin-right: 20rpx;
                }
            }
        }
    }
}

.tknum {
    .nut-popup {
        padding-top: 80rpx;
    }

    .btn {
        text-align: center;

        .nut-button {
            width: 60%;
            margin: 20rpx auto;
        }
    }
}
</style>